<?php

class Backdoor_IndexController extends Zend_Controller_Action {

    public function init() {
        $this->_statusMessenger = $this->_helper->flashMessenger;
        $this->_errorMessenger = new Zend_Controller_Action_Helper_FlashMessenger();
        $this->_errorMessenger->setNamespace('error');
        $this->view->statusMessages = $this->_statusMessenger->getMessages();
    }
    public function indexAction() {
      
    }
    
    public function loginAction() {

        $this->view->headTitle('Login');
        $this->view->documentClasses = array('index', 'login');
         $authAdapter = new Zend_Auth_Adapter_DbTable(
                        Zend_Db_Table::getDefaultAdapter(),
                        'adviser',
                        'username',
                        'password'
        );
        
        $usermapper= new Application_Model_AdviserMapper();
        
        if (Zend_Auth::getInstance()->hasIdentity()->role=='admin')  {
        
            $this->_statusMessenger->addMessage('Sie sind eingeloggt.');
            $this->_helper->redirector('Index', 'index');
        }
        $this->view->form = $this->_getLoginForm();
        $this->view->errorMessages = $this->_errorMessenger->getMessages();
    }

    public function checkLoginAction() {
        // Zurueck zu index, falls kein POST-Request
        // hereinkommt
        if (!$this->_request->isPost()) {
            $this->_errorMessenger->addMessage('Sie k&ouml;nnen sich nur &uuml;ber das Login-Formular anmelden.');
            return $this->_helper->redirector('login');
        }

        $form = $this->_getLoginForm();

        // falls das Formular falsch ausgefuellt ist, zurueck!
        if (!$form->isValid($this->_request->getPost())) {
            $this->view->form = $form;
            return $this->_helper->redirector('index');
        }

        $authAdapter = new Zend_Auth_Adapter_DbTable(
                        Zend_Db_Table::getDefaultAdapter(),
                        'adviser',
                        'username',
                        'password'
        );
        $params = $form->getValues();
        $authAdapter->setIdentity($params['username'])
                ->setCredential(md5($params['password']));
       
        $auth = Zend_Auth::getInstance();
        $authValid = $auth->authenticate($authAdapter)->isValid();
  
        if (!$authValid) {
            $this->view->form = $form;
            $this->_errorMessenger->addMessage('Falsche Benutzerdaten.');
            return $this->_helper->redirector('login');
        }
        if($authValid){
             //Angemeldeten Benutzer speichern 
        $usertemp = $authAdapter->getResultRowObject();
        $auth->getStorage()->write($usertemp);   


            
            
        $this->_helper->redirector('index');
        //$this->_redirect($params['referer']);

        
              
        
        }}

    public function logoutAction() {
        Zend_Auth::getInstance()->clearIdentity();
        $refererUrl = $this->_request->getServer('HTTP_REFERER');
        $this->_redirect($refererUrl);
    }

    protected function _getLoginForm() {
        $form = new Zend_Form();
        $form->setAction('/backdoor/index/check-login')
                ->setMethod(Zend_Form::METHOD_POST)
                ->addElements(array(
                    array('text', 'username', array(
                            'required' => true,
                            'label' => 'Benutzername'
                    )),
                    array('password', 'password', array(
                            'required' => true,
                            'label' => 'Kennwort'
                    )),
                    array('hidden', 'referer', array(
                            'decorators' => array('ViewHelper'),
                            'value' => $this->_request->getServer('HTTP_REFERER')
                    )),
                    array('submit', 'submit', array(
                            'label' => 'anmelden'
                    ))
                ));
        return $form;
    }

}

